package com.sinoglobal.ningxia.activity.amusement;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.IBase;
import com.sinoglobal.ningxia.activity.MyCollectionActivity;
import com.sinoglobal.ningxia.beans.AmusementInfoVo;
import com.sinoglobal.ningxia.beans.BusinessDetailVo;
import com.sinoglobal.ningxia.beans.ImageVo;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.fragment.SettleDownInfoFragment;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.frame.ShareAbstractActivity;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.widget.HorizontialListView;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

/**
 * @Title: SettleDownDetailsActivity.java
 * @Package com.sinoglobal.ningxia.activity.amusement
 * @Description: TODO(安居详情界面)
 * @author czz
 * @date 2014-6-25 下午1:49:02
 */
public class SettleDownDetailsActivity extends ShareAbstractActivity implements IBase, OnClickListener {

	private HorizontialListView setledownDetailsLv;// 安居图片集合
	private ImageView setledownDetailsIv; // 安居 图片
	private LayoutParams lp_lv_item; // HorizontialListView 的item的 宽高
	private TextView setledownName; // 安居 名称
	private TextView setledownType; // 安居户型
	private TextView setledownPrice; // 安居售价
	private TextView setledownPhone; // 安居电话
	private TextView setledownAddress; // 安居 地址
	private TextView setledownPhoneNum; // 致电
	private TextView playledownPhone;
	private CheckBox setledownCollect; // 收藏
	private TextView setledownShard; // 分享
	private String houseid; // 安居id
	private FinalBitmap finalBitmap21, finalBitmap43; // 下载图片
	private Intent call_intent; // 跳转到拨号界面
	private AmusementInfoVo amustmentInfoVo; // 请求返回的信息
	private LayoutParams lp_lv;
	private HorizontialListViewAdapter horizontialListViewAdapter; // 图片adapter
	private boolean flag = true;
	private String isFromCollection = "";// 用于判断是否从我的收藏界面跳转过来

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		titleView.setText(getString(R.string.my_collection_settle));
		setContentView(LayoutInflater.from(this).inflate(R.layout.activity_settledown_details, null));
		houseid = getIntent().getStringExtra(SettleDownInfoFragment.HOUSEID);
		finalBitmap21 = new FinalBitmap(this);
		finalBitmap43 = new FinalBitmap(this);
		finalBitmap21.configLoadingImage(R.drawable.default21);
		finalBitmap21.configLoadfailImage(R.drawable.default21);
		finalBitmap43.configLoadingImage(R.drawable.default43);
		finalBitmap43.configLoadfailImage(R.drawable.default43);
		init();
		getData();
		showListener();
	}

	@Override
	public void init() {
		isFromCollection = getIntent().getStringExtra(FlyApplication.isFromCollection);
		setledownDetailsIv = (ImageView) findViewById(R.id.setledown_details_iv);
		setledownDetailsLv = (HorizontialListView) findViewById(R.id.setledown_details_lv);
		setledownType = (TextView) findViewById(R.id.setledown_type);
		setledownName = (TextView) findViewById(R.id.setledown_name);
		setledownAddress = (TextView) findViewById(R.id.setledown_address);
		setledownPrice = (TextView) findViewById(R.id.setledown_price);
		setledownPhone = (TextView) findViewById(R.id.setledown_phone);
		setledownPhoneNum = (TextView) findViewById(R.id.setledown_phone_num);
		setledownPhoneNum.setVisibility(View.GONE);
		playledownPhone = (TextView) findViewById(R.id.setledown_phone_play);
		playledownPhone.setVisibility(View.VISIBLE);
		setledownCollect = (CheckBox) findViewById(R.id.setledown_collect);
		setledownShard = (TextView) findViewById(R.id.setledown_shard);

		templateButtonLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//Auto-generated method stub
				if ("1".equals(isFromCollection)) {
					Intent intent = new Intent(SettleDownDetailsActivity.this, MyCollectionActivity.class);
					SettleDownDetailsActivity.this.startActivity(intent);
					SettleDownDetailsActivity.this.finish();
				} else {
					SettleDownDetailsActivity.this.finish();
				}
			}
		});
	}
	private  void getData(){
		// 根据安居id获取详细信息
				new ItktAsyncTaskWithDialog<Void, Void, AmusementInfoVo>(getString(R.string.load_detail), true, true) {
					@Override
					public AmusementInfoVo before(Void... params) throws Exception {
						return RemoteImpl.getInstance().getSettleDownDetails(houseid);
					}

					@Override
					public void after(AmusementInfoVo result) {
						if (result != null) {
							if (Code.CODE_SUCCESS == result.getCode()) {
								// 1、设置ImageView的宽高
								amustmentInfoVo = result;
								ItktLog.e("======AmusementInfoVo========"+amustmentInfoVo.toString());
								LayoutParams lp_iv = new LayoutParams(FlyApplication.widthPixels, (int) (FlyApplication.widthPixels * 289 / 641));
								setledownDetailsIv.setLayoutParams(lp_iv);
								finalBitmap21.display(setledownDetailsIv, ConnectionUtil.localUrl + result.getHouse_img());
								// 2、设置HorizontialListView的高度
								int imgwidth = FlyApplication.widthPixels / 3 - 45;
								if (result.getImglist() != null) {
									int imgsize = result.getImglist().size();
									if (imgsize < 4) {
										lp_lv = new LayoutParams(imgsize * imgwidth, imgwidth * 107 / 128);
									} else {
										lp_lv = new LayoutParams(LayoutParams.WRAP_CONTENT, imgwidth * 107 / 128);
									}
									lp_lv.setMargins(15, 15, 15, 0);
									setledownDetailsLv.setLayoutParams(lp_lv);
									lp_lv_item = new LayoutParams(imgwidth, (FlyApplication.widthPixels / 3 - 45) * 107 / 128);
									if (result.getImglist() != null) {
										horizontialListViewAdapter = new HorizontialListViewAdapter(result.getImglist());
										setledownDetailsLv.setAdapter(horizontialListViewAdapter);
									}
								} else {
									setledownDetailsLv.setVisibility(View.GONE);
								}
								// 3、设置详细信息
								setledownName.setText(result.getHouse_name());
								setledownType.setText(getString(R.string.house_type) + result.getHouse_type());
								setledownPrice.setText(getString(R.string.sell_price)+ result.getHouse_price() + getString(R.string.price_per_squire));
								setledownPhone.setText(getString(R.string.user_tel1) + result.getHouse_tel());
								setledownAddress.setText(getString(R.string.user_address1) + result.getHouse_address());
								call_intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + result.getHouse_tel()));
								call_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								if ("1".equals(result.getIs_collection())) {
									flag = false;
									setledownCollect.setChecked(true);
								} else {
									setledownCollect.setChecked(false);
								}
							}
						}
					}

					@Override
					public void exception() {
					}

				}.execute();
	}
	@Override
	public void showListener() {
		playledownPhone.setOnClickListener(this);
		setledownShard.setOnClickListener(this);
		setledownCollect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (isLoginIntentLoginActivity()) {
					if (arg1) {
						setledownCollect.setClickable(false);
						setCollect(String.valueOf(1));
					} else {
						setledownCollect.setClickable(false);
						setCollect(String.valueOf(2));
					}
				} else {
					setledownCollect.setChecked(false);
				}
			}
		});
		setledownDetailsLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(SettleDownDetailsActivity.this, PhotoBigActivity.class);
				BusinessDetailVo businessDetail = new BusinessDetailVo();
				ArrayList<String> list = new ArrayList<String>();
				for (int i = 0; i < amustmentInfoVo.getImglist().size(); i++) {
					list.add(amustmentInfoVo.getImglist().get(i).getImg_url());
				}
				businessDetail.setImglist(list);
				businessDetail.setSelectedImgPosition(arg2);
				ItktLog.i("=========settle  =====  businessDetailVo==" + businessDetail);
				intent.putExtra(BusinessDetailVo.class.getCanonicalName(), businessDetail);
				SettleDownDetailsActivity.this.startActivity(intent);
			}
		});
	}

	@Override
	public void onBackPressed() {
		//Auto-generated method stub
		if ("1".equals(isFromCollection)) {
			Intent intent = new Intent(SettleDownDetailsActivity.this, MyCollectionActivity.class);
			SettleDownDetailsActivity.this.startActivity(intent);
			SettleDownDetailsActivity.this.finish();
		} else {
			SettleDownDetailsActivity.this.finish();
		}
		super.onBackPressed();
	}

	private void setCollect(final String action) {
		new ItktOtherAsyncTask<Void, Void, RootVo>(false) {
			@Override
			public RootVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().add2DelCollect(FlyApplication.userId, Byte.parseByte(String.valueOf(5)), Byte.parseByte(action), houseid);
			}

			@Override
			public void after(RootVo result) {
				setledownCollect.setClickable(true);
				if (Code.CODE_SUCCESS == result.getCode()) {
					if ("1".equals(action)) {
						showShortToastMessage(getString(R.string.collectSuccess));
					} else {
						showShortToastMessage(getString(R.string.collectCancel));
					}
				}
			}

			@Override
			public void exception() {

			}
		}.execute();
	}

	class HorizontialListViewAdapter extends BaseAdapter {
		private ArrayList<ImageVo> images;

		HorizontialListViewAdapter(ArrayList<ImageVo> images) {
			this.images = images;
		}

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public ImageVo getItem(int arg0) {
			return images.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			LinearLayout layout = new LinearLayout(SettleDownDetailsActivity.this);
			layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			ImageView iv = new ImageView(SettleDownDetailsActivity.this); // 128
			iv.setPadding(5, 0, 5, 0); // *
			iv.setLayoutParams(lp_lv_item);
			layout.addView(iv);
			finalBitmap43.display(iv, ConnectionUtil.localUrl + images.get(arg0).getImg_url());
			return layout;
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.setledown_phone_play:
			if (!MContants.EMPTY_STRING.equals(amustmentInfoVo.getHouse_address())) {
				if (call_intent != null) {
					startActivity(call_intent);
				}
			} else {
				showShortToastMessage(getString(R.string.no_phone));
			}
			break;
		case R.id.setledown_collect:
			break;
		case R.id.setledown_shard:
			// 分享
			showPopwindow(true, amustmentInfoVo.getShare_content(), null, ConnectionUtil.MicDoorAdress);
			break;
		}
	}
}